{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 《Python数据挖掘方法及应用》PyDm\n",
    "### 【第1章 数据收集与分析软件】数据与练习1 \n",
    "#### **（请在#下面问题的空白处写出代码并输出结果）**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.1  cars数据集给出了1920年记录的汽车行驶速度speed和刹车距离dist的数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>speed</th>\n",
       "      <th>dist</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   speed  dist\n",
       "1      4     2\n",
       "2      4    10\n",
       "3      7     4\n",
       "4      7    22\n",
       "5      8    16"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pydataset import data    #加载PyDataset包 \n",
    "cars = data('cars')           #调用pydataset包中的数据框cars\n",
    "cars.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#(1)作speed与dist的散点图，并以此判断speed与dist之间是否大致呈线性关系。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#(2)计算speed与dist的相关系数并进行假设检验。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#(3)建立speed对dist的OLS回归模型，并给出常用统计量。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#(4)预测当speed=30时，dist等于多少。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.2  freeny数据集(来自PyDataset包)给出了从1962年第二季度到1971年第四季度共39条季度收入及其影响因素的数据，y代表季度收入，lag.quartely.revenue代表滞后一期的季度收入，price index为物价指数，income level为收入水平，market potential为市场潜力。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>y</th>\n",
       "      <th>lag.quarterly.revenue</th>\n",
       "      <th>price.index</th>\n",
       "      <th>income.level</th>\n",
       "      <th>market.potential</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1962.25</td>\n",
       "      <td>8.79236</td>\n",
       "      <td>8.79636</td>\n",
       "      <td>4.70997</td>\n",
       "      <td>5.82110</td>\n",
       "      <td>12.9699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1962.50</td>\n",
       "      <td>8.79137</td>\n",
       "      <td>8.79236</td>\n",
       "      <td>4.70217</td>\n",
       "      <td>5.82558</td>\n",
       "      <td>12.9733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1962.75</td>\n",
       "      <td>8.81486</td>\n",
       "      <td>8.79137</td>\n",
       "      <td>4.68944</td>\n",
       "      <td>5.83112</td>\n",
       "      <td>12.9774</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1963.00</td>\n",
       "      <td>8.81301</td>\n",
       "      <td>8.81486</td>\n",
       "      <td>4.68558</td>\n",
       "      <td>5.84046</td>\n",
       "      <td>12.9806</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1963.25</td>\n",
       "      <td>8.90751</td>\n",
       "      <td>8.81301</td>\n",
       "      <td>4.64019</td>\n",
       "      <td>5.85036</td>\n",
       "      <td>12.9831</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               y  lag.quarterly.revenue  price.index  income.level  \\\n",
       "1962.25  8.79236                8.79636      4.70997       5.82110   \n",
       "1962.50  8.79137                8.79236      4.70217       5.82558   \n",
       "1962.75  8.81486                8.79137      4.68944       5.83112   \n",
       "1963.00  8.81301                8.81486      4.68558       5.84046   \n",
       "1963.25  8.90751                8.81301      4.64019       5.85036   \n",
       "\n",
       "         market.potential  \n",
       "1962.25           12.9699  \n",
       "1962.50           12.9733  \n",
       "1962.75           12.9774  \n",
       "1963.00           12.9806  \n",
       "1963.25           12.9831  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "freeny = data('freeny')\n",
    "freeny.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#(1)建立一个多元回归模型，用price index、income level和market potential三个变量解释y，所有解释变量各自都是统计显著的吗？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#(2)估计当price index=4.27789，income level=6.20030，market potential=13.1664时y的值。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#(3)预测当price index=5，income level=7，market potential=14时y的值。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.3 InsectSprays数据集(来自PyDataset包)给出了不同种类杀虫剂杀虫效果的数据。count为杀灭害虫的数量，spray为不同种类的杀虫剂。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>spray</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>20</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>14</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>14</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   count spray\n",
       "1     10     A\n",
       "2      7     A\n",
       "3     20     A\n",
       "4     14     A\n",
       "5     14     A"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "InsectSprays = data('InsectSprays')\n",
    "InsectSprays.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 问：不同杀虫剂的杀虫效果是否不同？建立一个回归模型进行分析。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.4 Loblolly数据集给出了松树生长的数据。height为松树的高度；age为松树的树龄；seed为松树的不同树种，它决定了松树能生长的最高高度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# (1)age是什么类型的变量？如何将它按四分位数转化为有序变量？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# (2)构建height与age、seed的多元回归模型，分析树龄、树种与树高的关系。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
